/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ywb.edugui.view.subject;

import com.ywb.edugui.controller.ControllerFactory;
import com.ywb.edugui.controller.SubjectController;
import com.ywb.edugui.dao.impl.SubjectDaoImpl;
import com.ywb.edugui.entity.Subject;
import com.ywb.edugui.utils.R;

import javax.swing.*;
import java.awt.*;

import static javax.swing.JOptionPane.*;

/**
 *
 * @author 10147
 */
public class SubjectQueryDialog extends javax.swing.JDialog {
    private SubjectController subjectController = ControllerFactory.getSubjectController(this);
    private Subject subject;

    public SubjectQueryDialog(java.awt.Frame parent, boolean modal, Subject s) {
        super(parent, modal);
        subject = s;
        initComponents();
        initTextField();
        setLocationRelativeTo(null);
    }

    private void setAllEditable(boolean b) {
        Component[] components = getRootPane().getContentPane().getComponents();
        for(Component component : components){
            if(component instanceof JTextField){
                ((JTextField) component).setEditable(b);
                component.setBackground(b == true ? Color.WHITE : Color.LIGHT_GRAY);
            }
        }
    }
    private void setAllEditable(boolean b, JTextField... textFields) {
        for(JTextField textField : textFields){
            textField.setEditable(b);
            textField.setBackground(b == true ? Color.WHITE : Color.LIGHT_GRAY);
        }
    }

    private void initTextField(){
        tfId.setText(subject.getId());
        tfTitle.setText(subject.getTitle());
        tfLevel.setText(subject.getParent_id().equals("0") ? "一级科目" : "二级科目");
        tfFirSubject.setText(subject.getTitle());
        if(subject.getParent_id().equals("0")){
            tfLevel.setText("一级科目");
            tfFirSubject.setText(subject.getTitle());
        }else{
            tfLevel.setText("一级科目");
            Subject parent = new SubjectDaoImpl().getOne(subject.getParent_id());
            tfFirSubject.setText(parent.getTitle());
        }
        setAllEditable(false);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jlId = new javax.swing.JLabel();
        jlTitle = new javax.swing.JLabel();
        jlLevel = new javax.swing.JLabel();
        jlFirstSubject = new javax.swing.JLabel();
        tfId = new javax.swing.JTextField();
        tfTitle = new javax.swing.JTextField();
        tfFirSubject = new javax.swing.JTextField();
        tfLevel = new javax.swing.JTextField();
        jbExitOrUpdate = new javax.swing.JButton();
        jbUpdate = new javax.swing.JButton();
        jbDelete = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jlId.setText("科目id：");

        jlTitle.setText("科目标题：");

        jlLevel.setText("科目级别：");

        jlFirstSubject.setText("所属科目");

        tfId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfIdActionPerformed(evt);
            }
        });

        jbExitOrUpdate.setText("返回");
        jbExitOrUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExitActionPerformed(evt);
            }
        });

        jbUpdate.setText("更新");
        jbUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbUpdateActionPerformed(evt);
            }
        });

        jbDelete.setText("删除");
        jbDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(81, 81, 81)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jlFirstSubject)
                                                .addGap(18, 18, 18)
                                                .addComponent(tfFirSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jlLevel)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(tfLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jlTitle)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(tfTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jlId)
                                                .addGap(18, 18, 18)
                                                .addComponent(tfId, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jbExitOrUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(jbUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jbDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(96, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jbUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jbDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(31, 31, 31)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(tfId, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                                        .addComponent(jlId, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(tfTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                                        .addComponent(jlTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jlLevel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(tfLevel, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jlFirstSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(tfFirSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(27, 27, 27)
                                .addComponent(jbExitOrUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(79, 79, 79))
        );

        pack();
    }// </editor-fold>

    private void tfIdActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void jbExitActionPerformed(java.awt.event.ActionEvent evt) {
        if(jbExitOrUpdate.getText().equals("返回")) {
            dispose();
        }
//        更新科目
        subject.setTitle(tfTitle.getText());
        if(subjectController.update(subject).getSuccess() == false){
            showMessageDialog(getParent(), "更新失败...");
            return ;
        }
        showMessageDialog(getParent(), "更新成功!!");
        dispose();

    }

    private void jbUpdateActionPerformed(java.awt.event.ActionEvent evt) {
        if(jbUpdate.getText().equals("更新")){
//            标题框设置为可编辑;
            setAllEditable(true, tfTitle);
            jbUpdate.setText("取消");
            jbExitOrUpdate.setText("确定更新");
        }else{
            setAllEditable(false);
            jbUpdate.setText("更新");
            jbExitOrUpdate.setText("返回");
        }
    }

    private void jbDeleteActionPerformed(java.awt.event.ActionEvent evt) {
        if(showConfirmDialog(this, "确定要删除 " + tfTitle.getText() + "科目么？") != YES_OPTION){
            return;
        }
        if(subjectController.delete(subject).getSuccess() == false){
            showMessageDialog(this, "删除失败...");
            return;
        }
        showMessageDialog(this, "删除成功!");
        dispose();
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jbDelete;
    private javax.swing.JButton jbExitOrUpdate;
    private javax.swing.JButton jbUpdate;
    private javax.swing.JLabel jlFirstSubject;
    private javax.swing.JLabel jlId;
    private javax.swing.JLabel jlLevel;
    private javax.swing.JLabel jlTitle;
    private javax.swing.JTextField tfFirSubject;
    private javax.swing.JTextField tfId;
    private javax.swing.JTextField tfLevel;
    private javax.swing.JTextField tfTitle;
    // End of variables declaration
}
